/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dto.mapping;

import cz.muni.fi.pa165.creatures.dto.CreatureTypeDTO;
import cz.muni.fi.pa165.creatures.dto.mapping.api.MappingDTO;
import cz.muni.fi.pa165.creatures.model.CreatureType;
import cz.muni.fi.pa165.creatures.model.DefenseStrategy;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * This class represents the mapping between
 * {@link cz.muni.fi.pa165.creatures.model.CreatureType} and
 * {@link cz.muni.fi.pa165.creatures.dto.CreatureTypeDTO} and vice versa.
 *
 * @author smikloso
 */
public class CreatureTypeMapping implements
        MappingDTO<CreatureTypeDTO, CreatureType> {

    @Autowired
    private CreatureMapping creatureMapping;

    @Override
    public CreatureTypeDTO entityToDTO(CreatureType creatureType) {
        if (creatureType == null) {
            return null;
        }

        CreatureTypeDTO dto = new CreatureTypeDTO();
        dto.setId(creatureType.getId().toString());
        dto.setName(creatureType.getName());
        dto.setDescription(creatureType.getDescription());
        dto.setDefenseStrategy(creatureType.getDefenseStrategy().toString());
        dto.setMaxHitPoints(creatureType.getMaxHitPoints());
        dto.setCreaturesInGroup(creatureType.getCreaturesInGroup());

        return dto;
    }

    @Override
    public CreatureType DTOtoEntity(CreatureTypeDTO dto) {
        if (dto == null) {
            return null;
        }

        CreatureType creatureType = new CreatureType();

        if (dto.getId() != null) {
            creatureType.setId(Long.parseLong(dto.getId()));
        }

        creatureType.setName(dto.getName());
        creatureType.setDescription(dto.getDescription());

        if (dto.getDefenseStrategy() == null) {
            creatureType.setDefenseStrategy(DefenseStrategy.NONE);
        } else {
            String strategy = dto.getDefenseStrategy().toUpperCase();
            if (strategy.equalsIgnoreCase("ATTACK")) {
                creatureType.setDefenseStrategy(DefenseStrategy.ATTACK);
            }

            if (strategy.equalsIgnoreCase("CLOAK")) {
                creatureType.setDefenseStrategy(DefenseStrategy.CLOAK);
            }

            if (strategy.equalsIgnoreCase("RETREAT")) {
                creatureType.setDefenseStrategy(DefenseStrategy.RETREAT);
            }

            if (strategy.equalsIgnoreCase("SUICIDE_ATTACK")) {
                creatureType.setDefenseStrategy(DefenseStrategy.SUICIDE_ATTACK);
            }

            if (strategy.equalsIgnoreCase("NONE")) {
                creatureType.setDefenseStrategy(DefenseStrategy.NONE);
            }
        }
        creatureType.setCreaturesInGroup(dto.getCreaturesInGroup());
        creatureType.setMaxHitPoints(dto.getMaxHitPoints());

        return creatureType;
    }
}
